/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using EvaluationException = System.Qizx.Api.EvaluationException;
using QName = System.Qizx.Api.QName;
using System.Qizx.XQuery;
using GenericValue = System.Qizx.XQuery.Dt.GenericValue;
namespace System.Qizx.XQuery.Op
{
	
	/// <summary> Superclass of Let For Case clauses.</summary>
	public class VarClause:Expression
	{
		public QName variable;
		public XQType declaredType; // check type
		public Expression expr; // source: in or :=
		
		public LocalVariable varDecl;
		public Expression owner; // FLWR, quantifiedExpr, typeswitch
		public bool checked_Renamed = false;
		public bool updating;
		
		public VarClause(QName variable)
		{
			this.variable = variable;
		}
		
		public override Expression Child(int rank)
		{
			return (rank == 0)?expr:null;
		}
		
		public override void  Dump(ExprDisplay d)
		{
			d.header(this);
			d.property("variable", variable);
			d.property("varAddress", varDecl.address);
			d.property("declType", declaredType);
			d.child("expr", expr);
		}
		
		// dummy sequence used as source for FLWR pipeline
		// Produces ONE dummy item.
		// also root class of For and Let sequences
		public class SingleDummy:GenericValue
		{
			virtual internal XQValue Source
			{
				set
				{
					this.source = value;
				}
				
			}
			internal XQValue source; // outer clause
			internal EvalContext context;
			internal Focus focus;
			internal bool done = false;
			
			
			public SingleDummy(Focus focus, EvalContext context)
			{
				this.focus = focus;
				this.context = context;
			}
			
			// single shot sequence
			public override bool Next()
			{
				if (done)
					return false;
				return done = true;
			}
			
			public override XQValue BornAgain()
			{
				return new SingleDummy(focus, context);
			}
		}
	}
}